package leetcode.code0010;

import leetcode.helper.HelpDebug;

/**
 * 有问题的解
 * @author CaoYu
 * @date 2021年9月20日
 * @description
 */
public class Solution_temp_bak extends Solution10 {

	@Override
	public boolean isMatch(String s, String p) {
		char[] chars1 = s.toCharArray();
		char[] chars2 = p.toCharArray();
		int len1 = chars1.length;
		int len2 = chars2.length;
		boolean[][] dp = new boolean[len1 + 1][len2 + 1];
		dp[0][0] = true;
		for (int i = 1; i <= len1; i++) {
			dp[i][0] = false;
		}
//		[a,a,a,]
//      [.,*,]
		for (int i = 1; i <= len2; i++) {
			dp[0][i] = false;
		} // chars2[i - 1] == '.' ? true : (chars2[i - 1] == '*' && i > 1 && dp[0][i - 1])
			// ? true : false
		for (int i = 1; i <= len1; i++) {
			for (int j = 1; j <= len2 && j <= i; j++) {
				if (chars1[i - 1] == chars2[j - 1]) {// 当前字符相等,
//					[a,a]
//					[a,a]
					if (dp[i - 1][j - 1]) {
						dp[i][j] = true;// 当前字符相等,之前也匹配
					} else {
// 						"aaabaaaababcbccbaab"
//						"c*c*.*c*a*..*c*"
						dp[i][j] = false;
						int index = j - 2;
						while (index > 0) {
							if (chars2[index] == '*') {
								if (dp[i - 1][index - 1]) {
									dp[i][j] = true;
									break;
								}
							} else {
								break;
							}
							index -= 2;
						}
					}
				} else {
//					"ab";
//					"a.";
					if (chars2[j - 1] == '.') {// 当前字符不等,用.匹配
						if (dp[i - 1][j - 1]) {
							dp[i][j] = true;
						} else {
							dp[i][j] = false;
							int index = j - 2;
							while (index > 0) {
								if (chars2[index] == '*') {
									if (dp[i - 1][index - 1]) {
										dp[i][j] = true;
										break;
									}
								} else {
									break;
								}
								index -= 2;
							}
						}
					} else if (chars2[j - 1] == '*') {// 当前字符不等,用*匹配
						// a
						// a*
						if (dp[i][j - 1]) {// *当0用,*没啥用
							dp[i][j] = true;
						} else
						// caa
						// ca*
						// aaa
						// .*
						if (dp[i - 1][j] && (chars1[i - 1] == chars2[j - 2] || chars2[j - 2] == '.')) {
							dp[i][j] = true;
						}
						// 不是 dp[i - 1][j - 1]
						else {
							dp[i][j] = false;
							int index = j - 1;
							while (index > 0) {
								if (chars2[index] == '*') {
									if (dp[i - 1][index - 1]) {
										dp[i][j] = true;
										break;
									}
								} else {
									break;
								}
								index -= 2;
							}
						}
					}
				}
			}
		}
		HelpDebug.print(chars1);
		HelpDebug.print(chars2);
		HelpDebug.print(dp);
		return dp[len1][len2];
	}

	public static void main(String[] args) {
		Solution_temp_bak so = new Solution_temp_bak();
//		so.debug1();
//		so.debug2();
//		so.debug3();
//		so.debug4();
		so.debug5();
//		so.myBug1();
//		so.myBug2();
//		so.myBug3();
//		so.myBug4();
//		so.myBug5();
//		so.myBug6();
	}

}
